package com.hy.study.driver;

import com.hy.study.combiner.WordCountCombiner;
import com.hy.study.mapper.WordCountMapper;
import com.hy.study.reduce.WordCountReducer;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.compress.BZip2Codec;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.GzipCodec;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineFileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @author huyong
 * @date 2024 年 01 月 29 日 11:44: 01
 */
public class WordCountDriver {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        String inputfile = "./input/nline_word_count";
        String outputfile = "./output/result1";
        // 1.获取Job对象
        Configuration conf = new Configuration();
        //开启map 输出文件的压缩
        conf.set("mapreduce.map.output.compress","true");
        //设置map端压缩方式
        conf.setClass("mapreduce.map.output.compress.codec", BZip2Codec.class, CompressionCodec.class);
        //设置reducer输出的压缩
        //开启reducer输出压缩
        conf.set("mapreduce.output.fileoutputformat.compress","true");
        //设置reducer输出压缩的算法
        conf.setClass("mapreduce.output.fileoutputformat.compress.codec", BZip2Codec.class, CompressionCodec.class);
        //设置压缩的方式 按行还是块 取值有三种  NONE, RECORD or BLOCK.
        conf.set("mapreduce.output.fileoutputformat.compress.type","BLOCK");
        Job job = Job.getInstance(conf);
        // 如果不设置，默认是FileInputFormat 合并小文件
        // job.setInputFormatClass(CombineFileInputFormat.class);
        // 设置存储切片最大值设置
        //  CombineFileInputFormat.setMaxInputSplitSize(job, 4194304);// 4M
        // 2.设置jar 存储位置
        job.setJarByClass(WordCountDriver.class);
        // 3.关联map 和reduce类
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);
        // 4.设置mapper 阶段输出数据的key 和value 类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        // 5.设置最终数据输出的key和value 类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        // 6.设置输入路径和输出路径
        FileInputFormat.addInputPath(job, new Path(inputfile));
        FileOutputFormat.setOutputPath(job, new Path(outputfile));
        //这里也可以设置开启输出的压缩方式
        FileOutputFormat.setCompressOutput(job,true);
        FileOutputFormat.setOutputCompressorClass(job, GzipCodec.class);
        // 设置 Combiner帮助reducer计算
        // job.setCombinerClass(WordCountCombiner.class);
        // 因为WordCountCombiner和WordCountReducer一模一样 可以使用
        job.setCombinerClass(WordCountReducer.class);// 与 job.setCombinerClass(WordCountCombiner.class);相同
        // 7.提交job 如果为true 提交完成后会答应信息
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }
}
